<?php
App::uses('AppController', 'Controller');
/**
 * Matches Controller
 *
 * @property Match $Match
 */
class MatchesController extends AppController {

public $uses = array('Match','Team');
public $paginate = array(
    'limit' => 25,
    'order' => array(
        'Match.id' => 'asc'
    )
);
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Match->recursive = 0;
		$this->set('matches', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Match->exists($id)) {
			throw new NotFoundException(__('Invalid match'));
		}
		$options = array('conditions' => array('Match.' . $this->Match->primaryKey => $id));
		$this->set('match', $this->Match->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Match->create();
			$this->request->data['Match']['name'] = $this->request->data['Match']['name1'].' vs '.$this->request->data['Match']['name2'];
			if ($this->Match->save($this->request->data)) {
				$this->Session->setFlash(__('The match has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The match could not be saved. Please, try again.'));
			}
		}
		$homes = $this->Match->home->find('list');
		$this->set(compact('homes'));
		$visitors = $this->Match->visitor->find('list');
		$this->set(compact('visitors'));
		$championships = $this->Match->Championship->find('list');
		$this->set(compact('championships'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Match->exists($id)) {
			throw new NotFoundException(__('Invalid match'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Match->save($this->request->data)) {
				$this->Session->setFlash(__('The match has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The match could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Match.' . $this->Match->primaryKey => $id));
			$this->request->data = $this->Match->find('first', $options);
		}
		
		$homes = $this->Match->home->find('list');
		$this->set(compact('homes'));
		$visitors = $this->Match->visitor->find('list');
		$this->set(compact('visitors'));
		$championships = $this->Match->Championship->find('list');
		$this->set(compact('championships'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Match->id = $id;
		if (!$this->Match->exists()) {
			throw new NotFoundException(__('Invalid match'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Match->delete()) {
			$this->Session->setFlash(__('Match deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Match was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	
	// Funcion que hace el Json para Sin Transmision
	public function st($date) {
		if (strcmp($date,'today') == 0){
			$date = date('Y-m-d', time());
		}
		
		$this->layout = 'default_ini';
		$final['timezone'] = date('O',time());
		$partidos = $this->Match->find('all',array('conditions' => array('DATEDIFF(Match.start,"'.$date.'") >= ' => 0), 'order' => array('Match.start ASC'), 'limit' => 5));
		$temp = array();
		$final['matches'] = array();
		foreach ($partidos as $game){

			if ($game['Match']['home'] == null || $game['Match']['visitor'] == null){
				$temp["t1"] = -1;
				$temp["t2"] = -1;
			}
			else{
				$temp["t1"] = (int)$game['Match']['home'];
				$temp["t2"] = (int)$game['Match']['visitor'];
			}
			if ($game['Match']['ended'] == 1){
				$temp["s1"] = (int)$game['Match']['scoreA'];
				$temp["s2"] = (int)$game['Match']['scoreB'];
			}
			$temp["e"] = (int)$game['Match']['ext'];
			$temp["time"] = $game['Match']['start'];
			array_push($final['matches'],$temp);
			unset($temp);
		}

		
		//$final['matches'] = $temp;
		if ( isset($_GET['var2'])){
		$final['nombre'] = $_GET['var2'];
		}
		$this->response->type('json');
		$this->response->body(json_encode($final));
		return $this->response;
	}
	
	// Funcion que intercambia visitante con local de un partido
	
	public function swap($id = null) {
		if (!$this->Match->exists($id)) {
			throw new NotFoundException(__('Invalid match'));
		}
		$this->Match->id = $id;
		$temp = $this->Match->findById($id);
		$this->Match->set('home',$temp['Match']['visitor']);
		$this->Match->set('visitor',$temp['Match']['home']);
		$name = explode("vs",$temp['Match']['name']);
		$this->Match->set('name',$name[1].' vs '.$name[0]);
		if ($this->Match->save()){
			$this->Session->setFlash(__('The match has been saved'));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('The match could not be saved. Please, try again.'));
			$this->redirect(array('action' => 'index'));
		}
	
	}
	
}
